package view;

import java.awt.Color;
import java.awt.Rectangle;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import controller.LocationListener;
import model.courtier.*;
import model.donnee.Client;
import model.donnee.Film;
import model.singleton.*;

@SuppressWarnings("serial")
public class LocationFilms extends JFrame {
	
	// JLabels
	JLabel lblNomFilm = new JLabel("Titre du film: ");
	JLabel lblDateLocation = new JLabel("Date de location: ");
	JLabel lblDateRetour = new JLabel("Date de retour: ");
	JLabel lblForfaitActuel = new JLabel("Forfait Actuel: ");
	JLabel lblNbLocRestantes = new JLabel("Locations restantes: ");
	JLabel lblErrorConfirm = new JLabel("Vous ne pouvez louer plus de films.");
	
	
	// Content
	JButton btnLocation = new JButton("Confirmer");
	
	public LocationFilms(int idFilm, int idClient)
	{
		Film rentMovie = DAOFilm.getFilm(idFilm);
		Client member = DAOClient.getClient(idClient);
		Film[] moviesRented = DAOLocation.getClientLocations(idClient);
		
		this.setTitle("Location de " + rentMovie.getTitre());
		this.setBounds(new Rectangle(400,260));
		
		// Format the labels
		this.lblNomFilm.setBounds(5, 10, 400, 20);
		this.lblDateLocation.setBounds(5, 30, 400, 20);
		this.lblDateRetour.setBounds(5, 50, 400, 20);
		this.lblForfaitActuel.setBounds(5, 70, 400, 20);
		this.lblNbLocRestantes.setBounds(5, 90, 400, 20);
		this.btnLocation.setBounds(140, 150, 120, 35);
		this.lblErrorConfirm.setBounds(100, 190, 200, 20);
		this.lblErrorConfirm.setForeground(Color.RED);
		
		// Set the information into the form
		this.lblNomFilm.setText(this.lblNomFilm.getText() + rentMovie.getTitre() + " (" + rentMovie.getIdfilm() + ")");
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		this.lblDateLocation.setText(this.lblDateLocation.getText() + sdf.format(cal.getTime()));
		cal.add(Calendar.DATE, (int)member.getForfait().getDureemax());
		this.lblDateRetour.setText(this.lblDateRetour.getText() + sdf.format(cal.getTime()));
		this.lblForfaitActuel.setText(this.lblForfaitActuel.getText() + member.getForfait().getTypeforfait());		
		int numLocLeft = (member.getForfait().getLocationmax() - moviesRented.length);
		if(!member.getForfait().getIdforfait().equals('A'))
		{
			this.lblNbLocRestantes.setText(this.lblNbLocRestantes.getText() + numLocLeft);
		}
		else
		{
			this.lblNbLocRestantes.setText(this.lblNbLocRestantes.getText() + "Illimité");
		}

		
		this.btnLocation.addActionListener(new LocationListener(idClient, idFilm, lblDateLocation,lblDateRetour,lblDateRetour, this));
		
		this.add(this.lblNomFilm);
		this.add(this.lblDateLocation);
		this.add(this.lblDateRetour);
		this.add(this.lblForfaitActuel);
		this.add(this.lblNbLocRestantes);
		this.add(this.btnLocation);
		this.add(this.lblErrorConfirm);
		this.add(new JLabel(""));
		
		this.lblErrorConfirm.setVisible(false);
		System.out.println(numLocLeft);
		if(numLocLeft == 0)
		{
			this.btnLocation.setEnabled(false);
			this.lblErrorConfirm.setVisible(true);
		}
		
		this.setVisible(true);
	}
	
}
